<template>
    <el-container class="pagebody">
        <!-- 头部区域 -->
        <el-header>
            home组件
            <el-button @click="loginout">退出</el-button>
        </el-header>
        <!-- 页面主体区域 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse ? '64px' : '200px'">
              <el-menu  background-color="#333744" text-color="#fff" active-text-color="#409eff" :collapse="isCollapse" :collapse-transition="false" unique-opened router :default-active="activePath">
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <!-- 一级菜单区域 -->
                <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
                  <template slot="title">
                    <i :class="iconsObj[item.id]"></i>
                    <span>{{item.authName}}</span>
                  </template>
                  <!-- <el-menu-item-group> -->
                    <!-- 二级菜单区域 -->
                    <el-menu-item :index="'/' + subitem.path" v-for="subitem in item.children" :key="subitem.id" @click="saveNavState('/' + subitem.path)">
                      <i class="el-icon-menu"></i>
                      <span>{{subitem.authName}}</span>
                    </el-menu-item>
                  <!-- </el-menu-item-group> -->
                </el-submenu>
              </el-menu>
            </el-aside>
            <!-- 右侧内容区域 -->
            <el-main>
              <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单数据
      menulist: [],
      iconsObj: {
        125: 'el-icon-s-custom',
        103: 'el-icon-s-check',
        101: 'el-icon-s-shop',
        102: 'el-icon-shopping-cart-2',
        145: 'el-icon-s-data'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.activePath
  },
  methods: {
    loginout () {
      // 清空token
      window.sessionStorage.clear()
      // 跳转到登录页
      this.$router.push('/login')
    },
    // 获取所有菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      console.log(res)
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存数据当前二级菜单地址
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = window.sessionStorage.activePath
    }
  }
}
</script>

<style lang="less" scoped>
.pagebody {
    height: 100%;
}
.el-header {
    display: flex;
    background-color: #373d41;
    justify-content: space-between;
    align-items: center;
    color: #fff;
}
.el-aside {
    background-color: #333744;
}
.el-main {
    background-color: #eaedf1;
}
.toggle-button {
  color: #fff;
  text-align: center;
  letter-space: 0.2em;
  cursor: pointer;
}
</style>
